package cn.smbms.service.user;

import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
    private UserMapper userMapper;

	@Override
	public User login(String userCode, String userPassword) throws Exception {
		User user = null;
		user = userMapper.getLoginUser(userCode);
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
				
		}
		return user;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public int getUserCount() {
		int result = 0;
		try {
			result = userMapper.getUserCount();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List<User> getUserList(String queryUserName, int queryUserRole,
			int currentPageNo, int pageSize) {
		int beginIndex = (currentPageNo-1)*pageSize;
		return userMapper.getUserList(queryUserName, queryUserRole, beginIndex,
				pageSize);
	}

	@Override
	public boolean add(User user) {
		boolean result = false;
		try {
			if (userMapper.add(user) == 1){
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public User getUserById(Integer id) {
		User user = null;
		try {
			user = userMapper.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean modify(User user) {
		boolean result = false;
		try {
			if (userMapper.modify(user) == 1) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public int updatePwd(int id, String pwd) {
		return userMapper.updatePwd(id,pwd);
	}

	@Override
	public int deleteUserById(int id) {
		return userMapper.deleteUserById(id);
	}

}
